package operation

import (
	"../common"
	"../web"
	"fmt"
	"strconv"
	"strings"
)

type OperPim2323Obj struct {
	common.BaseHandler
}

func (self *OperPim2323Obj) Prepare() {
	wono := strings.TrimSpace(self.Ctx.Params["wono"])
	line := strings.TrimSpace(self.Ctx.Params["line"])
	item := strings.TrimSpace(self.Ctx.Params["item"])
	station := strings.TrimSpace(self.Ctx.Params["station"])
	limit := strings.TrimSpace(self.Ctx.Params["limitval"])

	self.TmplData["line"] = line
	self.TmplData["wono"] = wono
	self.TmplData["item"] = item
	self.TmplData["station"] = station
	self.TmplData["limitval"] = limit
	lmt_ls := strings.Split(limit, ",")
	var lmt_idx []int
	for i, _ := range lmt_ls {
		lmt_idx = append(lmt_idx, i+1)
	}
	self.TmplData["limitlist"] = lmt_idx
}

func (self *OperPim2323Obj) ParameterCheck() bool {
	s_ssn := strings.TrimSpace(self.Ctx.Params["ssn"])
	ich1 := strings.TrimSpace(self.Ctx.Params["ich1"])
	ich2 := strings.TrimSpace(self.Ctx.Params["ich2"])
	ich3 := strings.TrimSpace(self.Ctx.Params["ich3"])
	ich4 := strings.TrimSpace(self.Ctx.Params["ich4"])
	ich5 := strings.TrimSpace(self.Ctx.Params["ich5"])
	ich6 := strings.TrimSpace(self.Ctx.Params["ich6"])
	ich7 := strings.TrimSpace(self.Ctx.Params["ich7"])
	ich8 := strings.TrimSpace(self.Ctx.Params["ich8"])
	ich9 := strings.TrimSpace(self.Ctx.Params["ich9"])
	ich10 := strings.TrimSpace(self.Ctx.Params["ich10"])
	limit := strings.TrimSpace(self.TmplData["limitval"].(string))
	limit_list := strings.Split(limit, ",")
	length := len(limit_list)
	if length == 0 {
		length = 4
	}

	var ls_lmt []float64
	var values []string
	for i := 0; i < length; i++ {
		ls_lmt = append(ls_lmt, -107.0)
		switch i {
		case 0:
			values = append(values, ich1)
		case 1:
			values = append(values, ich2)
		case 2:
			values = append(values, ich3)
		case 3:
			values = append(values, ich4)
		case 4:
			values = append(values, ich5)
		case 5:
			values = append(values, ich6)
		case 6:
			values = append(values, ich7)
		case 7:
			values = append(values, ich8)
		case 8:
			values = append(values, ich9)
		case 9:
			values = append(values, ich10)
		}
	}
	if limit != "" {
		for i, v := range limit_list {
			c_limit, err := strconv.ParseFloat(strings.TrimSpace(v), 32)
			if err != nil {
				ls_lmt[i] = -107.0
			} else {
				ls_lmt[i] = c_limit
			}
		}
	}

	if self.TmplData["wono"].(string) == "" {
		common.ForbiddenHandler(self.Ctx)
		return false
	}
	if s_ssn == "" {
		self.TmplData["error"] = common.GetText(self.Lang, "Please input the Product No.")
		return false
	}
	for i, v := range values {
		sflg := fmt.Sprintf("IMD(3rd)(dBm) CH%d", i+1)
		if v == "" {
			self.TmplData["error"] = common.GetText(self.Lang, "Please input %v", sflg)
			return false
		}
		val, err := strconv.ParseFloat(v, 32)
		if err != nil || val > ls_lmt[i] {
			self.TmplData["error"] = common.GetText(self.Lang, "%v is invalid.", sflg)
			return false
		}
		if val < -150.0 {
			self.TmplData["error"] = common.GetText(self.Lang, "%s value can not be less than -150.0", sflg)
			return false
		}
		if val > 0.0 {
			self.TmplData["error"] = common.GetText(self.Lang, "%s value can not be more than 0.0", sflg)
			return false
		}
	}

	self.Sqlparas = append(self.Sqlparas, s_ssn)
	self.Sqlparas = append(self.Sqlparas, self.TmplData["wono"])
	self.Sqlparas = append(self.Sqlparas, self.TmplData["station"])
	self.Sqlparas = append(self.Sqlparas, self.TmplData["line"])
	self.Sqlparas = append(self.Sqlparas, self.TmplData["item"])
	self.Sqlparas = append(self.Sqlparas, ich1)
	self.Sqlparas = append(self.Sqlparas, ich2)
	self.Sqlparas = append(self.Sqlparas, ich3)
	self.Sqlparas = append(self.Sqlparas, ich4)
	self.Sqlparas = append(self.Sqlparas, ich5)
	self.Sqlparas = append(self.Sqlparas, ich6)
	self.Sqlparas = append(self.Sqlparas, ich7)
	self.Sqlparas = append(self.Sqlparas, ich8)
	self.Sqlparas = append(self.Sqlparas, ich9)
	self.Sqlparas = append(self.Sqlparas, ich10)
	self.Sqlparas = append(self.Sqlparas, self.Usr.Username)
	self.Flashparas = []interface{}{strings.ToUpper(s_ssn)}

	return true
}

func (self *OperPim2323Obj) SqlExecute() bool {
	spRtnData := *common.FetchOneRowPtr(
		`sp_TestDataForPim ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?`,
		self.Usr.Conn, new(common.SPReturn), self.Sqlparas...)
	if spRtnData.(common.SPReturn).Flag > 0 {
		self.TmplData["error"] = common.GetTextSP(self.Lang, spRtnData)
		return false
	}
	return true
}

func Pim2323Handler(ctx *web.Context) {
	self := new(OperPim2323Obj)
	self.Ctx = ctx
	self.Html = "2323PIM.html"
	self.Rightlist = []string{"scanflow"}
	self.Flashstr = "Save [ %v ] finished."
	self.DoRender(self)
}
